package com.choupin.choupin.admin.controller;


import com.choupin.choupin.admin.entity.Administration;
import com.choupin.choupin.admin.entity.Personal;
import com.choupin.choupin.admin.service.IAdminService;
import com.choupin.choupin.controller.BaseController;
import com.choupin.choupin.entity.JsonResult;
import com.choupin.choupin.entity.Mine;
import com.choupin.choupin.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@SuppressWarnings("ALL")
@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController extends BaseController {


    @Autowired
    private IAdminService adminService;

//    注册
    @CrossOrigin
    @PostMapping("/register")
    public JsonResult<Void> register(@RequestBody Map<String,Object> data  ){
        JsonResult<Void> jsonResult=new JsonResult<>();
        System.out.println(data.get("phone")+","+data.get("gender"));
        Administration administration=new Administration();
        administration.setPhone((String) data.get("phone"));
        administration.setPassword((String) data.get("password"));
        administration.setGender((Integer) data.get("gender"));
        administration.setAuthentication((String) data.get("code"));
        String dataString= (String) data.get("birthday");
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date=simpleDateFormat.parse(dataString);
            administration.setBirthday(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        adminService.register(administration);
        jsonResult.setState(SUCCESS);
        return jsonResult;
    }

//    登录
    @CrossOrigin
    @PostMapping("/login")
    public JsonResult<Void> login(@RequestBody Map<String,Object> data ){
        JsonResult<Void> jsonResult=new JsonResult<>();
        adminService.login((String) data.get("phone"), (String) data.get("password"));
        jsonResult.setState(SUCCESS);
        return  jsonResult;
    }

    //     查看个人信息
    @CrossOrigin
    @PostMapping("/show")
    public JsonResult<Personal> showPersonal(@RequestBody Map<String,Object> data){
        JsonResult<Personal> jsonResult=new JsonResult<>();
        Personal personal=adminService.showPersonal((String) data.get("phone"));
        System.out.println(personal);
        jsonResult.setData(personal);
        jsonResult.setState(SUCCESS);
        return  jsonResult;
    }

    //    更新头像
    @CrossOrigin
    @PostMapping("/uploadavatar")
    public JsonResult<Void> uploadAvatar(@RequestParam("file") MultipartFile file, @RequestParam("phone") String phone ) throws Exception {
        JsonResult<Void> jsonResult=new JsonResult<>();
        adminService.uploadAvatar(file,phone);
        jsonResult.setState(SUCCESS);
        return jsonResult;
    }


    //    更新个人信息
    @CrossOrigin
    @PostMapping("/updatepersonal")
    public JsonResult<Void> updatePersonal(@RequestBody Map<String,Object> data ){
        JsonResult<Void> jsonResult=new JsonResult<>();
        Administration administration=new Administration();
        System.out.println("更新个人信息能来到这里吗！"+(Integer) data.get("gender"));
        administration.setPhone((String) data.get("phone"));
        System.out.println("获取到的更新年龄："+(int) data.get("age"));
        administration.setAge((int) data.get("age"));
        administration.setNickname((String)data.get("nickname"));
        administration.setPassword((String) data.get("password"));
        administration.setGender((Integer) data.get("gender"));
        System.out.println("更新性别为："+administration.getGender());
        String birthdaystr= (String)data.get("birthday");
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date birthday=simpleDateFormat.parse(birthdaystr);
            administration.setBirthday(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        adminService.updatePersonal(administration,(String) data.get("newPassword"),(String)data.get("confirmPassword"));
        jsonResult.setState(SUCCESS);
        return  jsonResult;
    }

    //    注销
    @CrossOrigin
    @PostMapping("/logout")
    public  JsonResult<Void> logOut(@RequestBody Map<String,Object> data ){
        JsonResult<Void> jsonResult=new JsonResult<>();
        adminService.logout((String) data.get("phone"));
        jsonResult.setState(SUCCESS);
        return jsonResult;
    }

    /* 该方法用户实现登录拦截功能
     * */
    @CrossOrigin
    @PostMapping("/adminloginerror")
    public JsonResult<Void> loginerror(@RequestBody Map<String,Object> data){
        //获取前端传来的手机号
        String phone= (String) data.get("phone");
        System.out.println("前端传来的手机号："+phone);
        JsonResult<Void> jsonResult=new JsonResult<>();
        adminService.loginInterceptor(phone);
        jsonResult.setState(SUCCESS);
        return jsonResult;
    }



}
